import { post } from "@/util/request";
import { goReLaunch } from "@/static/ts/common";
import { SysTime } from "@/api/SystemApi";
import { initializeWebSocket, disconnectWebSocket } from "@/util/websocket";

// 用户登录
export const Logon = async (data : any) => {
	const obj = await post("/UserApi/Logon", data);
	await getUserInfo(obj);
};
// 发送注册验证码
export const SendRegisterVerificationCode = async (data : any) => {
	const obj = await post("/UserApi/SendRegisterVerificationCode", data);
};
// 注冊
export const UserRegister = async (data : any) => {
	const obj = await post("/UserApi/UserRegister", data);
	getUserInfo(obj);
};
// 获取用户信息并且跳转
export const getUserInfo = async (obj : any) => {
	uni.showLoading({
		mask: true
	})
	uni.setStorageSync("userInfo", obj);
	await UserInfo();
	uni.showLoading({
		mask: true
	})
	await SysTime();
	initializeWebSocket();
	uni.hideLoading()



	goReLaunch("/pages/index/index", "登錄成功");
};

// 用戶信息
export const UserInfo = async () => {
	let obj = await post("/UserApi/UserInfo");
	if (uni.getStorageSync("userInfo")) {
		obj = {
			...uni.getStorageSync("userInfo"),
			...obj,
		};
	}
	uni.setStorageSync("userInfo", obj);
	return obj;
};
// 修改资料
export const UserInfoUpdate = async (data : any) => {
	const obj = await post("/UserApi/UserInfoUpdate", data);
	uni.showToast({ title: "更新成功", icon: "none" });
	UserInfo();
};
// 退出登录
export const LogOut = async () => {
	await post("/UserApi/LogOut");
	disconnectWebSocket();
	goReLaunch("/pages/notLoggedIn/login", "退出登录");
};

// 设置交易密码
export const PaymentPwSet = async (data : any) => {
	await post("/UserApi/PaymentPwSet", data);
};

// 修改交易密码
export const PaymentPwUpdate = async (data : any) => {
	await post("/UserApi/PaymentPwUpdate", data);
};
// 修改登录密码
export const PwUpdate = async (data : any) => {
	await post("/UserApi/PwUpdate", data);
};
// 用户余额
export const UserMoney = async () => {
	let obj = await post("/UserApi/UserMoney");
	uni.setStorageSync("balance", obj.balance);
	return obj.balance;
};
// 账变订单
export const UserOrder = async (data : any) => {
	let obj = await post("/UserApi/UserOrder", data);
	return obj;
};
// 账变类型
export const OrderType = async () => {
	let obj = await post("/UserApi/OrderType");
	return obj;
};
// 银行类型列表
export const BankCategory = async () => {
	let arr = await post("/UserApi/BankCategory");
	return arr;
};

// 用户提现银行卡添加
export const UserWithdrawalBankCardAdd = async (data : any) => {
	let obj = await post("/UserApi/UserWithdrawalBankCardAdd", data);
	return obj;
};
// 用户提现银行卡删除
export const UserWithdrawalBankCardDel = async (data : any) => {
	let obj = await post("/UserApi/UserWithdrawalBankCardDel", data);
	return obj;
};

// 用户提现银行卡列表
export const UserWithdrawalBankCard = async () => {
	let obj = await post("/UserApi/UserWithdrawalBankCard");
	return obj;
};
// 提现
export const UserWithdrawal = async (data : any) => {
	let obj = await post("/UserApi/UserWithdrawal", data);
	return obj;
};
// 网银支付通道列表
export const BankPayment = async () => {
	let obj = await post("/UserApi/BankPayment");

	let obj2 = await QuickPayment();
	return [...obj, ...obj2];
};
// 快捷支付列表
export const QuickPayment = async () => {
	let obj = await post("/UserApi/QuickPayment");
	return obj;
};

// 网银支付下单
export const BankPaymentPlaceOrder = async (data : any) => {
	let obj = await post("/UserApi/BankPaymentPlaceOrder", data);
	return obj;
};
// 快捷支付下单
export const QuickPaymentPlaceOrder = async (data : any) => {
	let obj = await post("/UserApi/QuickPaymentPlaceOrder", data);
	return obj;
};

// 提现记录
export const UserWithdrawalBankCardOrder = async (data : any) => {
	let obj = await post("/UserApi/UserWithdrawalBankCardOrder", data);
	return obj;
};

// 充值记录
export const UserRechargeOrder = async (data : any) => {
	let obj = await post("/UserApi/UserRechargeOrder", data);
	return obj;
};